var express = require('express');
var router = express.Router();
const sql = require('../db')


/* GET users listing. */
router.get('/goods', function(req, res, next) { //获取商品信息

    sql('select * from goods', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/shopping_cart', function(req, res, next) { //获取购物车商品信息

    sql('select * from shopping_cart', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});

router.get('/mzh/shopping_cart', function(req, res, next) {//清空购物车商品
  
    sql('delete from shopping_cart',[])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});

router.post('/addedshopping', (req, res) => {
    //新增购物车商品
    if (req.body) {
        sql('insert into shopping_cart set ?', req.body).then((result) => {
            if (result.affectedRows > 0) {
                // console.log(result.in)
                res.json({ code: 200, data: "新增成功" });
            } else {
                res.json({ code: 400, data: "新增失败" })
            }
        }).catch((err) => {
            // console.log(err)
            res.json({ code: 500, data: "程序出错" });
        })
    }
});
router.delete('/:id', (req, res) => { //删除购物车商品
    sql('delete from shopping_cart where id=?', req.params.id)
        .then((result) => {
            if (result.affectedRows > 0) {
                res.json({ code: 200, data: "删除用户成功" })

            } else {
                res.json({ code: 400, data: "删除用户失败" })
            }
        }).catch((err) => {
            res.json({ code: 500, data: "程序出错" })
        })
});

router.put("/mzh/:id", (req, res) => {
    if (!isNaN(req.params.id)) { //修改购物车中商品的数量
        sql(`update shopping_cart set ?
   where id=?`, [req.body, req.params.id])
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: "修改成功" })
                } else {
                    res.json({ code: 400, data: "修改失败" })
                }
            }).catch((err) => {
                console.log(err);
                res.json({ code: 500, data: "程序出错" })
            })
    } else {
        res.json({ code: 400, data: "编号不正确" })
    }
});



router.put("/:id", (req, res) => {
    if (!isNaN(req.params.id)) { //减少用户金币余额，或者给用户爸爸充钱
        console.log(req.params.id)
        sql(`update user_info set ?
   where id=?`, [req.body, req.params.id])
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: "修改成功" })
                } else {
                    res.json({ code: 400, data: "修改失败" })
                }
            }).catch((err) => {
                console.log(err);
                res.json({ code: 500, data: "程序出错" })
            })
    } else {
        res.json({ code: 400, data: "编号不正确" })
    }
});
//获取指定兑换码的金额
router.get('/mzh/:code', (req, res) => {

    if (!isNaN(req.params.code)) {
        sql("select * from code where code=?", req.params.code)
            .then((result) => {
                res.json({ code: 200, data: result[0] });
            }).catch(() => {
                res.json({ code: 500, data: "程序出错" });
            })
    } else {
        res.json({ code: 400, data: "编号不正确" })
    }
});
//历史记录
router.get('/xu', function(req, res, next) {
  
    sql('select * from xu', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/yweek', function(req, res, next) {
  
    sql('select * from yweek', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/Week', function(req, res, next) {
  
    sql('select * from Week', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/tweek', function(req, res, next) {
  
    sql('select * from tweek', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});


//user获取用户表的所有数据
// router.get('/', function(req, res, next) {
//     // res.send('respond with a resource');
//     sql('select * from user_info', [])
//         .then((result) => {
//             res.json({ code: 200, data: result });
//         }).catch((err) => {
//             console.log(err);
//             res.json({ code: 500, data: "程序错误" })
//         })
// });

// 获取指定id的用户
// router.get('/:id', (req, res) => {
//     // req.query req.boby post req.paramms
//     if (!isNaN(req.params.id)) {
//         sql("select * from user_info where id=?", req.params.id)
//             .then((result) => {
//                 res.json({ code: 200, data: result[0] });
//             }).catch(() => {
//                 res.json({ code: 500, data: "程序出错" });
//             })
//     } else {
//         res.json({ code: 400, data: "编号不正确" })
//     }
// })

// 验证登录操作
router.post('/login', (req, res) => {
        sql("select * from user_info where username='" + req.body.username + "' and password='" + req.body.password + "'")
            .then((result) => {
                if (result.length == 0) {
                    res.json({ code: 300, data: "用户名或密码错误" })
                } else {
                    res.json({ code: 200, data: result[0], token: 1, username: req.body.username });
                }
            }).catch(() => {
                res.json({ code: 500, data: "程序出错" });
            })

    })
    // 查询金币数量
router.get('/:username', (req, res) => {
        // req.query req.boby post req.paramms
        // if (!isNaN(req.params.username)) {
        sql("select * from user_info where username=?", req.params.username)
            .then((result) => {
                res.json({ code: 200, data: result[0] });
            }).catch(() => {
                res.json({ code: 500, data: "程序出错" });
            })
    }
    //  else {
    //     res.json({ code: 400, data: "用户名不正确" })
    // }
)

// 删除用户信息
router.delete('/:username', (req, res) => {
        sql('delete from user_info where username=?', req.params.username)
            .then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: "删除用户成功" })
                } else {
                    res.json({ code: 400, data: "删除用户失败" })
                }
            }).catch((err) => {
                res.json({ code: 500, data: "程序出错" })
            })
    })
    // 修改用户名 {username:xxx}
router.put("/:id", (req, res) => {
    sql(`update user_info set ? where id=? `, [req.body, req.params.id])
        .then((result) => {
            if (result.affectedRows > 0) {
                res.json({ code: 200, data: "修改成功" })
            } else {
                res.json({ code: 400, data: "修改失败" })
            }
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序出错" })
        })
})

// 注册用户
router.post('/register', (req, res) => {
        if (req.body) {
            sql("insert into user_info set ?", req.body).then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: "新增成功" });
                } else {
                    res.json({ code: 400, data: "新增失败" })
                }
            }).catch(() => {
                res.json({ code: 500, data: "程序出错" });
            })
        }
    })
    // 修改性别
    // router.put("/:username", (req, res) => {
    //     sql(`update user_info set sex=? where username=? `, [req.body.sex, req.params.username])
    //         .then((result) => {
    //             if (result.affectedRows > 0) {
    //                 res.json({ code: 200, data: "修改成功" })
    //             } else {
    //                 res.json({ code: 400, data: "修改失败" })
    //             }
    //         }).catch((err) => {
    //             console.log(err);
    //             res.json({ code: 500, data: "程序出错" })
    //         })
    // })

// 新增用户
// router.post('/', (req, res) => {
//     if (req.body) {
//         sql("insert into user set ?", req.body).then((result) => {
//             if (result.affectedRows > 0) {
//                 res.json({ code: 200, data: "新增成功" });
//             } else {
//                 res.json({ code: 400, data: "新增失败" })
//             }
//         }).catch(() => {
//             res.json({ code: 500, data: "程序出错" });
//         })
//     }
// })
// 用户修改PUT/user/101
// 修改数据中需要包含元数据的主键
// 两种方式 url上传id 或者表单中包括id
// router.put("/:id", (req, res) => {
//     if (!isNaN(req.params.id)) {
//         sql(`update user set username=?,
//   password=?,status=? where id=?`, [req.body.username, req.body.password, req.body.status, req.params.id])
//             .then((result) => {
//                 if (result.affectedRows > 0) {
//                     res.json({ code: 200, data: "修改成功" })
//                 } else {
//                     res.json({ code: 400, data: "修改失败" })
//                 }
//             }).catch((err) => {
//                 console.log(err);
//                 res.json({ code: 500, data: "程序出错" })
//             })
//     } else {
//         res.json({ code: 400, data: "编号不正确" })
//     }
// })
module.exports = router;